In [1]:
x=5
y="John"
print(type(x))
print(type(y))
<class 'int'>
<class 'str'>
In [1]:
thislist = ["apple", "banana", "cherry"]
print(thislist)
['apple', 'banana', 'cherry']
In [2]:
thislist = ["apple", "banana", "cherry", "apple", "cherry"]
print(thislist)
['apple', 'banana', 'cherry', 'apple', 'cherry']
In [4]:
list1 = ["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]

print(list1)
print(list2)
print(list3)
['apple', 'banana', 'cherry']
[1, 5, 7, 9, 3]
[True, False, False]
In [5]:
thislist = list(("apple", "banana", "cherry"))
print(thislist)
['apple', 'banana', 'cherry']
In [6]:
thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
  print("Yes, 'apple' is in the fruits list")
Yes, 'apple' is in the fruits list
In [7]:
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
banana
In [8]:
thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)
['apple', 'blackcurrant', 'cherry']
In [9]:
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
thislist[1:3] = ["blackcurrant", "watermelon"]
print(thislist)
['apple', 'blackcurrant', 'watermelon', 'orange', 'kiwi', 'mango']
In [10]:
thislist = ["apple", "banana", "cherry"]
thislist.insert(2, "watermelon")
print(thislist)
['apple', 'banana', 'watermelon', 'cherry']
In [11]:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
['apple', 'banana', 'cherry', 'orange']
In [12]:
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)
['apple', 'orange', 'banana', 'cherry']
In [13]:
thislist = ["apple", "banana", "cherry"]
tropical = ["mango", "pineapple", "papaya"]
thislist.extend(tropical)
print(thislist)
['apple', 'banana', 'cherry', 'mango', 'pineapple', 'papaya']
In [14]:
thislist = ["apple", "banana", "cherry"]
thistuple = ("kiwi", "orange")
thislist.extend(thistuple)
print(thislist)
['apple', 'banana', 'cherry', 'kiwi', 'orange']
In [15]:
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
['apple', 'cherry']
In [16]:
thislist = ["apple", "banana", "cherry"]
thislist.pop(1)
print(thislist)
['apple', 'cherry']
In [17]:
thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)
[]

thislist = ["apple", "banana", "cherry"] for i in range(len(thislist)): print(thislist[i])

In [19]:
thislist = ["apple", "banana", "cherry"]
i = 0
while i < len(thislist):
  print(thislist[i])
  i = i + 1
apple
banana
cherry
In [20]:
thislist = ["apple", "banana", "cherry"]
[print(x) for x in thislist]
apple
banana
cherry
Out[20]:
[None, None, None]
In [21]:
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []

for x in fruits:
  if "a" in x:
    newlist.append(x)

print(newlist)
['apple', 'banana', 'mango']
In [23]:
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]

newlist = [x for x in fruits if x != "apple"]

print(newlist)
['banana', 'cherry', 'kiwi', 'mango']
In [24]:
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]

newlist = [x.upper() for x in fruits]

print(newlist)
['APPLE', 'BANANA', 'CHERRY', 'KIWI', 'MANGO']
In [25]:
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)
['banana', 'kiwi', 'mango', 'orange', 'pineapple']
In [26]:
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort(reverse = True)
print(thislist)
['pineapple', 'orange', 'mango', 'kiwi', 'banana']
In [27]:
def myfunc(n):
  return abs(n - 50)

thislist = [100, 50, 65, 82, 23]
thislist.sort(key = myfunc)
print(thislist)
[50, 65, 23, 82, 100]
In [28]:
thislist = ["banana", "Orange", "Kiwi", "cherry"]
thislist.sort()
print(thislist)
['Kiwi', 'Orange', 'banana', 'cherry']
In [29]:
thislist = ["banana", "Orange", "Kiwi", "cherry"]
thislist.reverse()
print(thislist)
['cherry', 'Kiwi', 'Orange', 'banana']
In [30]:
thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist)
['apple', 'banana', 'cherry']
In [31]:
thislist = ["apple", "banana", "cherry"]
mylist = list(thislist)
print(mylist)
['apple', 'banana', 'cherry']
In [32]:
thislist = ["apple", "banana", "cherry"]
mylist = thislist[:]
print(mylist)
['apple', 'banana', 'cherry']
In [33]:
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]

list3 = list1 + list2
print(list3)
['a', 'b', 'c', 1, 2, 3]
In [34]:
thistuple = ("apple", "banana", "cherry")
print(thistuple)
('apple', 'banana', 'cherry')
In [35]:
thistuple = ("apple", "banana", "cherry")
print(len(thistuple))
3
In [36]:
thistuple = ("apple",)
print(type(thistuple))

#NOT a tuple
thistuple = ("apple")
print(type(thistuple))
<class 'tuple'>
<class 'str'>
In [38]:
tuple1 = ("apple", "banana", "cherry")
tuple2 = (1, 5, 7, 9, 3)
tuple3 = (True, False, False)
print(tuple1)
print(tuple2)
print(tuple3)
('apple', 'banana', 'cherry')
(1, 5, 7, 9, 3)
(True, False, False)
In [39]:
thistuple = tuple(("apple", "banana", "cherry")) 
print(thistuple)
('apple', 'banana', 'cherry')
In [1]:
thistuple = ("apple", "banana", "cherry")
print(thistuple[1])
banana
In [2]:
thistuple = ("apple", "banana", "cherry")
print(thistuple[-1])
cherry
In [3]:
thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[2:5])
('cherry', 'orange', 'kiwi')
In [4]:
thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[-2:-5])
()
In [5]:
thistuple = ("apple", "banana", "cherry")
if "apple" in thistuple:
  print("Yes, 'apple' is in the fruits tuple")
Yes, 'apple' is in the fruits tuple
In [6]:
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)

print(x)
('apple', 'kiwi', 'cherry')
In [8]:
thistuple = ("apple", "banana", "cherry")
y = list(thistuple)
y.append("orange")
thistuple = tuple(y)
print(thistuple)
('apple', 'banana', 'cherry', 'orange')
In [10]:
thistuple = ("apple", "banana", "cherry")
y = list(thistuple)
y.remove("apple")
thistuple = tuple(y)
print(thistuple)
('banana', 'cherry')
In [11]:
fruits = ("apple", "banana", "cherry", "strawberry", "raspberry")

(green, yellow, *red) = fruits

print(green)
print(yellow)
print(red)
apple
banana
['cherry', 'strawberry', 'raspberry']
In [12]:
thistuple = ("apple", "banana", "cherry")
i = 0
while i < len(thistuple):
  print(thistuple[i])
  i = i + 1
apple
banana
cherry
In [13]:
fruits = ("apple", "banana", "cherry")
mytuple = fruits * 2

print(mytuple)
('apple', 'banana', 'cherry', 'apple', 'banana', 'cherry')
In [14]:
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)

tuple3 = tuple1 + tuple2
print(tuple3)
('a', 'b', 'c', 1, 2, 3)
In [15]:
thisset = {"apple", "banana", "cherry", "apple"}

print(thisset)
{'apple', 'cherry', 'banana'}
In [17]:
set1 = {"apple", "banana", "cherry"}
set2 = {1, 5, 7, 9, 3}
set3 = {True, False, False}
print(set1)
print(set2)
print(set3)
{'apple', 'cherry', 'banana'}
{1, 3, 5, 7, 9}
{False, True}
In [18]:
myset = {"apple", "banana", "cherry"}
print(type(myset))
<class 'set'>
In [19]:
thisset = {"apple", "banana", "cherry"}

for x in thisset:
  print(x)
apple
cherry
banana

thisset = {"apple", "banana", "cherry"}

thisset.add("orange")

print(thisset)

In [21]:
thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}

thisset.update(tropical)

print(thisset)
{'apple', 'pineapple', 'mango', 'banana', 'cherry', 'papaya'}
In [22]:
thisset = {"apple", "banana", "cherry"}
mylist = ["kiwi", "orange"]

thisset.update(mylist)

print(thisset)
{'apple', 'kiwi', 'banana', 'cherry', 'orange'}
In [23]:
thisset = {"apple", "banana", "cherry"}

thisset.remove("banana")

print(thisset)
{'apple', 'cherry'}

Heading¶

learing

*this the litalic*

mark this value

this is the updated sections


the values for increase the parts

this is blockquote

  1. python
  2. java
  3. Sql
  • HTML
  • Css
  • javascript

This is code

First Term
This is the definition of the first term.
Second Term
This is one definition of the second term.
This is another definition of the second term.

Join Set¶

Union¶

In [30]:
set1={"a","b","c"}
set2={1,2,3}
set3= set1.union(set2)
print(set3)
{1, 2, 'b', 'c', 3, 'a'}

Join muliple set¶

In [31]:
set1={"a","b","c"}
set2={1,3,5}
set3={"John","elena"}
set4={"apple","banana","cherry"}
myset=set1.union(set2,set4,set3)
print(myset)
{'apple', 1, 'b', 'c', 3, 5, 'banana', 'cherry', 'a', 'John', 'elena'}

Intersection¶

In [32]:
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}

set3 = set1.intersection(set2)
print(set3)
{'apple'}

difference¶

In [34]:
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}

set3 = set1.difference(set2)

print(set3)
{'banana', 'cherry'}

Dictionary¶

In [35]:
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(thisdict)
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}

Dictionary Items¶

In [36]:
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 2002
}
print(thisdict["brand"])
Ford

Duplicates Not Allowed¶

In [37]:
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 2003,
  "year": 2002
    
}
print(thisdict)
{'brand': 'Ford', 'model': 'Mustang', 'year': 2002}

Dictionary Items - Data Types¶

In [38]:
thisdict = {
  "brand": "Ford",
  "electric": False,
  "year": 1964,
  "colors": ["red", "white", "blue"]
}

print(thisdict)
{'brand': 'Ford', 'electric': False, 'year': 1964, 'colors': ['red', 'white', 'blue']}

The dict Constructor¶

In [39]:
thisdict = dict(name = "ABIMANYU", age = 22, country = "Indian")
print(thisdict)
{'name': 'ABIMANYU', 'age': 22, 'country': 'Indian'}

Accessing Items¶

In [40]:
thisdict =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 2003
}
x = thisdict["model"]
print(x)
Mustang

get key¶

In [43]:
thisdict =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
x = thisdict.keys()
print(x)
dict_keys(['brand', 'model', 'year'])

Get Items¶

In [45]:
thisdict =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
x = thisdict.items()
print(x)
dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])

Get values¶

In [46]:
thisdict =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
x = thisdict.values
print(x)
<built-in method values of dict object at 0x0000020FD434F840>

Check if key¶

In [47]:
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
if "model" in thisdict:
  print("Yes, 'model' is one of the keys in the thisdict dictionary")
Yes, 'model' is one of the keys in the thisdict dictionary

Update dictionary¶

In [48]:
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict.update({"year": 2020})

print(thisdict)
{'brand': 'Ford', 'model': 'Mustang', 'year': 2020}

Removing Items¶

In [49]:
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict.pop("model")
print(thisdict)
{'brand': 'Ford', 'year': 1964}

Loop Dictionary¶

In [50]:
thisdict ={
"brand" : "Ford",
    "model":"Mustang",
    "year":2003
}
for x in thisdict:
    print(x)
brand
model
year

Copy a Dictionary¶

In [51]:
thisdict = {
    "brand":"ford",
    "model":"Mustang",
    "year":2002
}
mydict=thisdict.copy()
print(mydict)
{'brand': 'ford', 'model': 'Mustang', 'year': 2002}

nested Dictionary¶

In [54]:
myfamily = {
  "child1" : {
    "name" : "abi",
    "year" : 2002
  },
  "child2" : {
    "name" : "aravindhan",
    "year" : 2004
  },
  "child3" : {
    "name" : "aasai",
    "year" : 2006
  }
}

print(myfamily)
{'child1': {'name': 'abi', 'year': 2002}, 'child2': {'name': 'aravindhan', 'year': 2004}, 'child3': {'name': 'aasai', 'year': 2006}}
In [55]:
myfamily = {
  "child1" : {
    "name" : "abi",
    "year" : 2002
  },
  "child2" : {
    "name" : "aravindhan",
    "year" : 2004
  },
  "child3" : {
    "name" : "aasai",
    "year" : 2006
  }
}

print(myfamily["child2"]["name"])
aravindhan

Loop though distinary¶

In [56]:
myfamily = {
  "child1" : {
    "name" : "abi",
    "year" : 2002
  },
  "child2" : {
    "name" : "aravindhan",
    "year" : 2004
  },
  "child3" : {
    "name" : "aasai",
    "year" : 2006
  }
}
for x, obj in myfamily.items():
  print(x)

  for y in obj:
    print(y + ':', obj[y])
child1
name: abi
year: 2002
child2
name: aravindhan
year: 2004
child3
name: aasai
year: 2006

If Else¶

In [57]:
a = 33
b = 200
if b > a:
  print("b is greater than a")
b is greater than a
In [62]:
a = 33
b = 200
if b > a:
  print("b is greater than a")
elif b == a:
    print("a and b are equal")
b is greater than a

Else¶

In [60]:
a = 200
b = 33
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
else:
  print("a is greater than b")
a is greater than b

Hand if else¶

In [63]:
a = 2
b = 330
print("A") if a > b else print("B")
B

and¶

In [64]:
a = 200
b = 33
c = 500
if a > b and c > a:
  print("Both conditions are True")
Both conditions are True

OR¶

In [65]:
a = 200
b = 33
c = 500
if a > b or c > a:
  print("At last conditions are True")
Both conditions are True

Not¶

In [66]:
a = 33
b = 200
if not a > b:
  print("a is NOT greater than b")
a is NOT greater than b

Nested if¶

In [ ]:
x = 40

if x > 10:
  print("Above ten,")
  if x > 20:
    print("and also above 20!")
  else:
    print("but not above 20.")

While loop¶

In [ ]:
i=1
while i<6:
    print(i)
    i +=1

break statement¶

In [1]:
i = 1
while i < 6:
  print(i)
  if i == 3:
    break
  i += 1
1
2
3

Continue¶

In [5]:
i = 0
while i < 6:
  i += 1
  if i == 3:
    continue
  print(i)
1
2
4
5
6

For loop¶

In [6]:
fruits=["apple","banana","cherry"]
for x in fruits:
    print(x)
apple
banana
cherry

Looping thought string¶

In [7]:
for x in "banana":
    print(x)
b
a
n
a
n
a

Break statement¶

In [9]:
fruits=["apple","banana","cherrry"]
for x in fruits:
    print(x)
    if x=="banana":
        break
apple
banana

Continue¶

In [12]:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
  if x == "banana":
    continue
  print(x)
apple
cherry

Range¶

In [13]:
for x in range(6):
    print(x)
0
1
2
3
4
5

Nested Loop¶

In [14]:
adj =["red","blue","pink"]
fruits=["apple","banana","cherry"]
for x in adj:
    for y in fruits:
        print (x,y)
red apple
red banana
red cherry
blue apple
blue banana
blue cherry
pink apple
pink banana
pink cherry

Calling Function¶

In [15]:
def my_function():
  print("Hello from a function")

my_function()
Hello from a function

Arguments¶

In [16]:
def my_function(fname):
  print(fname + " Refsnes")

my_function("Emil")
my_function("Tobias")
my_function("Linus")
Emil Refsnes
Tobias Refsnes
Linus Refsnes

Number of Arguments¶

In [17]:
def my_function(fname, lname):
  print(fname + " " + lname)

my_function("Emil", "Refsnes")
Emil Refsnes

Arbitrary Arguments, *args¶

In [18]:
def my_function(*kids):
  print("The youngest child is " + kids[2])

my_function("Emil", "Tobias", "Linus")
The youngest child is Linus

Keyword Arguments¶

In [19]:
def my_function(child3, child2, child1):
  print("The youngest child is " + child3)

my_function(child1 = "Emil", child2 = "Tobias", child3 = "Linus")
The youngest child is Linus

Arbitrary Keyword Arguments, **kwargs¶

In [20]:
def my_function(**kid):
  print("His last name is " + kid["lname"])

my_function(fname = "Tobias", lname = "Refsnes")
His last name is Refsnes

Default Parameter Value¶

In [21]:
def my_function(country = "Norway"):
  print("I am from " + country)

my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
I am from Sweden
I am from India
I am from Norway
I am from Brazil

Passing a List as an Argument¶

In [22]:
def my_function(food):
  for x in food:
    print(x)

fruits = ["apple", "banana", "cherry"]

my_function(fruits)
apple
banana
cherry

Return values¶

In [23]:
def my_function(x):
  return 5 * x

print(my_function(3))
print(my_function(5))
print(my_function(9))
15
25
45

Recursion¶

In [24]:
def tri_recursion(k):
  if(k > 0):
    result = k + tri_recursion(k - 1)
    print(result)
  else:
    result = 0
  return result

print("Recursion Example Results:")
tri_recursion(6)
Recursion Example Results:
1
3
6
10
15
21
Out[24]:
21

lambda¶

In [25]:
def myfunc(n):
  return lambda a : a * n

mytripler = myfunc(3)

print(mytripler(11))
33

Accessing Array element¶

In [26]:
cars = ["Ford", "Volvo", "BMW"]

x = cars[0]

print(x)
Ford

length of Array¶

In [27]:
cars = ["Ford", "Volvo", "BMW"]

x = len(cars)

print(x)
3

looping Array elements¶

In [29]:
cars = ["Ford", "Volvo", "BMW"]

for x in cars:
  print(x)
Ford
Volvo
BMW

Adding Array elements¶

In [34]:
cars = ["Ford", "Volvo", "BMW"]

cars.append("Honda")

print(cars)
['Ford', 'Volvo', 'BMW', 'Honda']

Removing Element¶

In [35]:
cars = ["Ford", "Volvo", "BMW"]

cars.pop(1)

print(cars)
['Ford', 'BMW']

Create Class¶

In [37]:
class MyClass:
  x = 5

print(MyClass)
<class '__main__.MyClass'>

init _Function¶

In [46]:
class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

p1 = Person("Abimanyu", 36)

print(p1.name)
print(p1.age)
Abimanyu
36

str_Function¶

In [47]:
class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

p1 = Person("John", 36)

print(p1)
<__main__.Person object at 0x0000023EAC919E20>

Object_method¶

In [48]:
class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def myfunc(self):
    print("Hello my name is " + self.name)

p1 = Person("Abimanyu", 20)
p1.myfunc()
Hello my name is Abimanyu

Self Parameter¶

In [45]:
class Person:
  def __init__(mysillyobject, name, age):
    mysillyobject.name = name
    mysillyobject.age = age

  def myfunc(abc):
    print("Hello my name is " + abc.name)

p1 = Person("Abimanyu", 36)
p1.myfunc()
Hello my name is Abimanyu

Modify Object Properties¶

In [50]:
class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def myfunc(self):
    print("Hello my name is " + self.name)

p1 = Person("Abimanyu", 26)

p1.age = 20

print(p1.age)
20

Create a Parent Class¶

In [51]:
class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

  def printname(self):
    print(self.firstname, self.lastname)

x = Person("Abimanyu", "aa")
x.printname()
Abimanyu aa

Create Child Class¶

In [52]:
class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

  def printname(self):
    print(self.firstname, self.lastname)

class Student(Person):
  pass
x = Student("Aravindhan", "K")
x.printname()
Aravindhan K

Add the init() Function¶

In [54]:
class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

  def printname(self):
    print(self.firstname, self.lastname)

class Student(Person):
  def __init__(self, fname, lname):
    Person.__init__(self, fname, lname)

x = Student("abi", "manyu")
x.printname()
abi manyu

super() Function¶

In [59]:
class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

  def printname(self):
    print(self.firstname, self.lastname)

class Student(Person):
  def __init__(self, fname, lname):
    super().__init__(fname, lname)

x = Student("Abimanyu", ".k")
x.printname()
Abimanyu .k

Add Properties¶

In [60]:
class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

  def printname(self):
    print(self.firstname, self.lastname)

class Student(Person):
  def __init__(self, fname, lname):
    super().__init__(fname, lname)
    self.graduationyear = 2019

x = Student("abimanyu", "aa")
print(x.graduationyear)
2019

Add Methods¶

In [63]:
class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

  def printname(self):
    print(self.firstname, self.lastname)

class Student(Person):
  def __init__(self, fname, lname, year):
    super().__init__(fname, lname)
    self.graduationyear = year

  def welcome(self):
    print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear)

x = Student("Abimanyu", "abi", 2004)
x.welcome()
Welcome Abimanyu abi to the class of 2004

Iterator¶

In [66]:
mytuple= ("apple","banana","cherry")
myit = iter(mytuple)
print(next(myit))
print(next(myit))
print(next(myit))
apple
banana
cherry

Looping though an Iterator¶

In [67]:
mytuple= ("apple","banana","cherry")
for x in mytuple:
    print(x)
apple
banana
cherry

Create an Iterator¶

In [68]:
class MyNumbers:
  def __iter__(self):
    self.a = 1
    return self

  def __next__(self):
    x = self.a
    self.a += 1
    return x

myclass = MyNumbers()
myiter = iter(myclass)

print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
1
2
3
4
5

Stop iterator¶

In [73]:
class MyNumbers:
  def __iter__(self):
    self.a = 1
    return self

  def __next__(self):
    if self.a <= 14:
      x = self.a
      self.a += 1
      return x
    else:
      raise StopIteration

myclass = MyNumbers()
myiter = iter(myclass)

for x in myiter:
  print(x)
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Polymorphism¶

In [76]:
class Car:
  def __init__(self, brand, model):
    self.brand = brand
    self.model = model

  def move(self):
    print("Drive!")

class Boat:
  def __init__(self, brand, model):
    self.brand = brand
    self.model = model

  def move(self):
    print("Sail!")

class Plane:
  def __init__(self, brand, model):
    self.brand = brand
    self.model = model

  def move(self):
    print("Fly!")

car1 = Car("Ford", "Mustang")       #Create a Car object
boat1 = Boat("Ibiza", "Touring 20") #Create a Boat object
plane1 = Plane("Boeing", "747")     #Create a Plane object

for x in (car1, boat1, plane1):
  x.move()
Drive!
Sail!
Fly!

inheritance Class Polymorphism¶

In [81]:
class Vehicle:
  def __init__(self, brand, model):
    self.brand = brand
    self.model = model

  def move(self):
    print("Move!")

class Car(Vehicle):
  pass

class Boat(Vehicle):
  def move(self):
    print("Sail!")

class Plane(Vehicle):
  def move(self):
    print("Fly!")

car1 = Car("Ford", "Mustang") #Create a Car object
boat1 = Boat("Ibiza", "Touring 20") #Create a Boat object
plane1 = Plane("Boeing", "747") #Create a Plane object

for x in (car1, boat1, plane1):
  print(x.brand)
  print(x.model)
  x.move()
Ford
Mustang
Move!
Ibiza
Touring 20
Sail!
Boeing
747
Fly!

Scope¶

Local Scope¶

In [83]:
def myfunc():
  x = 300
  print(x)

myfunc()
300

Global Scope¶

In [87]:
x = 300

def myfunc():
  print(x)

myfunc()

print(x)
300
300

Global Keyword¶

In [88]:
def myfunc():
  global x
  x = 300

myfunc()

print(x)
300

Nonlocal keyword¶

In [89]:
def myfunc1():
  x = "Jane"
  def myfunc2():
    nonlocal x
    x = "hello"
  myfunc2()
  return x

print(myfunc1())
hello
In [3]:
import numpy as np
arr =np.array([[[1,2,3],[4,5,6],[1,2,3],[4,5,6]]])
print(arr)
[[[1 2 3]
  [4 5 6]
  [1 2 3]
  [4 5 6]]]

3-D Array¶

In [7]:
import numpy as np
arr =np.array([[[1,2,3],[4,5,6],[1,2,3],[4,5,6]]])
print(arr)
[[[1 2 3]
  [4 5 6]
  [1 2 3]
  [4 5 6]]]

Check Number of Dimension¶

In [10]:
import numpy as np
a=np.array(42)
b=np.array([1,2,3,4,5])
c=np.array([[1,2,3],[4,5,6]])
d=np.array([[[1,2,3],[4,5,6],[1,2,3],[4,5,6]]])
print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)
0
1
2
3

higher Dimensional Array¶

In [15]:
import numpy as np

arr = np.array([1, 2, 3, 4], ndmin=5)

print(arr
print('number of dimensions :', arr.ndim)
[[[[[1 2 3 4]]]]]
number of dimensions : 5

array slicing¶

In [18]:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[1:5])
[2 3 4 5]

Negative slicing¶

In [22]:
import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[-3:-1])
[5 6]

STEP¶

In [25]:
import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[1:5:2])
[2 4]

slicing 2-D array¶

In [28]:
import numpy as np

arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])

print(arr[1, 1:4])
[7 8 9]

Data types¶

In [1]:
import numpy as np

arr = np.array([1, 2, 3, 4], dtype='i4')

print(arr)
print(arr.dtype)
[1 2 3 4]
int32

converting data type on exsiting array¶

In [4]:
import numpy as np

arr = np.array([1.1, 2.1, 3.1])

newarr = arr.astype('i')

print(newarr)
print(newarr.dtype)
[1 2 3]
int32

Array copy¶

In [7]:
import numpy as np
arr=np.array([1,2,3,4,5])
x=arr.copy()
arr[0]=42
print(arr)
print(x)
[42  2  3  4  5]
[1 2 3 4 5]

Array view¶

In [10]:
import numpy as np
arr=np.array([1,2,3,4,5])
x=arr.view()
arr[0]=42
print(arr)
print(x)
[42  2  3  4  5]
[42  2  3  4  5]

Check if array¶

In [13]:
import numpy as np
arr = np.array([1,2,3,4,5])
x = arr.copy()
y = arr.view()
print(x.base)
print(y.base)
None
[1 2 3 4 5]

Shape of an array¶

In [16]:
import numpy as np
arr = np.array([[1,2,3,4],[5,6,7,8]])
print(arr.shape)
(2, 4)

Return copy or view¶

In [19]:
import numpy as np
arr = np.array ([1,2,3,4,5,6,7,8])
print(arr.reshape(2,4).base)
[1 2 3 4 5 6 7 8]

unknown Dimension¶

In [22]:
import numpy as np
arr = np.array ([1,2,3,4,5,6,7,8])
newarr = arr.reshape(2,2,-1)
print(newarr)
[[[1 2]
  [3 4]]

 [[5 6]
  [7 8]]]

Flatttning array¶

In [25]:
import numpy as np
arr = np.array([[1,2,3],[4,5,6]])
newarr = arr.reshape(-1)
print(newarr)
[1 2 3 4 5 6]
In [ ]: